<?php

class OrderController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
			$input =Input::all();

			$validation=Order::validation($input);


		if($validation->fails()){
			return $message=$validation->messages();//->toArray(); 
		}else{
			

			//Category Id Available or not Check
				$Avail=DB::table('users')
		  			  ->where('UserId',$input['OrderUserID'])->first();			
			//end that
		  		if($Avail==null){
					return "You are not Logged in";
		  		}else{
		  			$user=Order::create($input);
					return "Data Added SuccessFully";
		  		}		  	

			//$user=Product::create($input);
			//return "Data Added SuccessFully";
		}
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
							
		$Order = DB::table('orders')->where('OrderID', $id)->first();
        if ($Order) {
          	
        		print_r($Order);
           // return Response::json(ResponseManager::getResult($feeStructure, $message));
        	
        } else {
           return $message = 'No Match Found';
           // return Response::json(ResponseManager::getError(array(), 01, $message));
        }
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
 	$data = DB::table('orders')->where('OrderID', $id)->first();
		//$data = Order::where('OrderID', '=', $id) ->first(); 
	      
	$lenth=count($data);		

	if($lenth=="1"){	
	      $input =Input::all();
		  
		  $validation=Order::validation($input);

		if($validation->fails()){
			return $message=$validation->messages();//->toArray(); 
		}else{
			//User logged or not 
				$Avail=DB::table('users')
		  			  ->where('UserId',$input['OrderUserID'])->first();			
		  		if($Avail==null){
					return "You are not Logged in";
			//end that
		  		}else{
		  			$order=DB::table('order')
				  			->where('OrderID', $id)
            			    ->update($input);
					return "Data updated SuccessFully";
		  		}		  	
		}
	}else{
			return "No Record Found";
		}
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//$Order = DB::table('orders')->where('OrderID', $id)->first();
         $order = Order::find($id);
        print_r($order);
        if($order){
          $order->delete();
        }else{
        	return "NO RECORD TO DELETE";
        	return Response::json(ResponseManager::getError(array(), 01, $message));
        	//return Response::json(ResponseManager::getResult($feeStructureData->toArray(), $message));
        }
        if ($order) {
         return   $message = 'Deleted Successfully.';
            //return Response::json(ResponseManager::getResult($feeStructureData->toArray(), $message));
        } else {
           return  $message = 'Ooopss...! Not deleted.';
            //return Response::json(ResponseManager::getError(array(), 01, $message));
        }
	}


}
